﻿namespace DimensionsHelper.Client.WinUi.ViewModels;

public partial class QueryDataViewModel : ObservableRecipient
{
    public ObservableCollection<QueryDataTableViewModel> Tables { get; } = [];


    public void AddDataTable(DataTable data, string metadata, string casedata, string query)
    {
        QueryDataTableViewModel table = new(data, Tables)
        {
            TableName = data.TableName,
            Metadata = $"元数据: {metadata}",
            Casedata = $"数据库: {casedata}",
            Query = $"筛选器: {query}"
        };

        App.DispatcherQueue.TryEnqueue(() =>
        {
            Tables.Add(table);
        });
    }


    public void RemoveDataTable(string? tableName)
    {
        int index = -1;
        for (int i = 0; i < Tables.Count; i++)
        {
            if (Tables[i].TableName == tableName)
            {
                index = i;
                break;
            }
        }

        if (index > -1)
        {
            App.DispatcherQueue.TryEnqueue(() =>
            {
                Tables.RemoveAt(index);
            });
        }
    }
}